package com.cennavi.bigdata.baotou.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.bigdata.baotou.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *
 * @packageName: com.zhuxl.exportdoc.controller
 * @className:
 * @description:
 * @author: 2810010108@qq.com
 * @createTime: 2018/6/23 14:18 with idea 2018.1
 * </p>
 * version: V1.0.0
 */
@Controller
@RequestMapping("report")
public class ReportController {

    @Autowired
    private ReportService reportService;


    @ResponseBody
    @RequestMapping(value = "/poverty_export_word",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String povertyExportWord(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "json",required = false) String json,
                                  @RequestParam(value = "info",required = false) String info) {

        JSONArray array = JSONObject.parseObject(info).getJSONArray("data");
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < array.size()-1; i++) {
            JSONObject json_i = (JSONObject) array.get(i);
            double avgindex_i = json_i.getDouble("avgindex");
            if (avgindex_i >= 0 && avgindex_i < 2) {
                json_i.put("avgindex", avgindex_i + " 畅通");
            } else if (avgindex_i >= 2 && avgindex_i < 4) {
                json_i.put("avgindex", avgindex_i + " 基本畅通");
            } else if (avgindex_i >= 4 && avgindex_i < 6) {
                json_i.put("avgindex", avgindex_i + " 缓慢");
            } else if (avgindex_i >= 6 && avgindex_i < 8) {
                json_i.put("avgindex", avgindex_i + " 轻度拥堵");
            } else if (avgindex_i >= 8) {
                json_i.put("avgindex", avgindex_i + " 拥堵加剧");
            }
            stringBuffer.append(json_i.getString("time")+",")
                        .append(json_i.getString("avgspeed")+",")
                        .append(json_i.getString("maxminspeed").replace("&","&amp;")+",")
                        .append(json_i.getString("avgindex")+",")
                        .append(json_i.getString("maxminindex").replace("&","&amp;")+",")
                        .append(json_i.getString("avgtraveltime")+",");
        }
        JSONObject json_2 = (JSONObject) array.get(2);
        stringBuffer.append(json_2.getString("time")+",")
                .append(json_2.getString("avgspeed")+",")
                .append(json_2.getString("maxminspeed").replace("&","&amp;")+",")
                .append(json_2.getString("avgindex")+",")
                .append(json_2.getString("maxminindex").replace("&","&amp;")+",")
                .append(json_2.getString("avgtraveltime"));
        info = json_2.getString("name")+","+stringBuffer.toString();
        JSONObject jsonObject = JSONObject.parseObject(json);
        jsonObject.put("info",info);
        String file = reportService.exportWord(jsonObject);

//        InputStream fin = null;
//        OutputStream out = null;
//        try {
//            // 调用工具类WordGeneratorUtils的createDoc方法生成Word文档
//            fin = new FileInputStream(file);
//            response.setCharacterEncoding("utf-8");
//            response.setContentType("application/msword");
//            // 设置浏览器以下载的方式处理该文件
//            // 设置文件名编码解决文件名乱码问题
//            response.addHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes(), "iso-8859-1"));
//            out = response.getOutputStream();
//            byte[] buffer = new byte[512];
//            int bytesToRead = -1;
//            // 通过循环将读入的Word文件的内容输出到浏览器中
//            while ((bytesToRead = fin.read(buffer)) != -1) {
//                out.write(buffer, 0, bytesToRead);
//            }
//        } catch (Exception e) {
//            throw new RuntimeException("导出失败", e);
//        } finally {
//            try {
//                if (fin != null) {
//                    fin.close();
//                }
//                if (out != null) {
//                    out.close();
//                }
//                if (file != null) {
//                    file.delete();
//                }
//            } catch (IOException e) {
//                throw new RuntimeException("导出失败", e);
//            }
//        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("url","http://15.32.12.109:8000/files/"+file);
        jsonObject1.put("errcode",0);
        return jsonObject1.toString() ;
    }

}
